
var GLOBAL = {};

var urlMap = url_spe;

/**
 * 转换
 * @param param
 * @returns {___anonymous828_829}
 */
function funcTranslateToJson(param){
	var obj = {};
	try{
		var arr = decodeURIComponent(param).split("&");
		$.each(arr, function(i,n){
			var subarr = n.split('=');
			obj[subarr[0]] = subarr[1];
		});
	}catch(e){
		console.log(e);
	}
	return obj;
}

function funcAppendSelectOptions(apiId){
	var options = $('#id_select').find('option');
	var addFlag = true;
	$.each(options, function(i,n){
		if($(n).val() == apiId){
			addFlag = false;
			return false;
		}
	});
	if(addFlag){
		var tpl = '<option value="{1}">{1}</option>';
		$('#id_select').append( tpl.replace(/\{1\}/g, apiId) );
	}
}

function funcChangeUrl(){
	var checkedVal = $('input[name="reqType"]:checked').val();
	var checkedEnv = $('input[name="env_radio"]:checked').val();
	$('#id_url').empty();
	$('#id_url').html(urlMap[checkedEnv][checkedVal]);
	$('#id_hidden_url').val(urlMap[checkedEnv][checkedVal]);
	if("Soap"==checkedVal){
		$.each($('input[name="Format_aa"]'), function(i,n){
			$(n).prop('checked', false);
			if('xml'==$(n).val()){
				$(n).prop('checked', true);
			}
		});
		$('input[name="Format_aa"]').trigger('change');
		$('input[name="Format_aa"]').prop('disabled',true);
	}
	if("Rest"==checkedVal){
		$('input[name="Format_aa"]').prop('disabled',false);
	}
}

$(function(){
	//
	//Rest/Soap切换
	$('input[name="reqType"]').unbind();
	$('input[name="reqType"]').on('change',function(){
		funcChangeUrl();
	});
	
	//env切换
	$('input[name="env_radio"]').unbind();
	$('input[name="env_radio"]').on('change',function(){
		var checked_env = $('input[name="env_radio"]:checked').val();
		$('#id_hidden_env').val( checked_env );
		funcChangeUrl();
	});
	
	//json/xml切换
	$('input[name="Format_aa"]').unbind();
	$('input[name="Format_aa"]').on('change',function(){
		var checkedVal = $('input[name="Format_aa"]:checked').val();
		$('#id_Format').empty();
		$('#id_Format').html(checkedVal);
		$('#id_hidden_Format').val(checkedVal);
	});
	
	//提交按钮
	$('#btn_submit').unbind();
	$('#btn_submit').on('click',function(){
		var now = moment();
		// yyyy-MM-dd HH:mm:ss:SSS
		var now_str = now.format('YYYY-MM-DD HH:mm:ss:SSS');
		$('#id_Time_Stamp').empty();
		$('#id_Time_Stamp').html(now_str);
		$('#id_hidden_Time_Stamp').val(now_str);
		
		var cache_serialize = $('#form1').serialize();
		var checked_env = $('input[name="env_radio"]:checked').val();
		// 做了修改: 下拉选项中加上env前缀
		var cache_apiId = '['+checked_env+']-' + $('#id_apiId').val();
		
//		return false;
		$.ajax({
			url: ctx+'/common/index/testStepSubmit',
			data: $('#form1').serialize(),
			type: 'post',
			dataType: 'json',
			success: function(data){
				if(data){
					if(data['errorCode'] == '0'){
						var foo = data['result'];
						$('#id_reqParam').val( foo['reqParam'] );
						$('#id_respResult').val( foo['respResult'] );
						if( foo['respResult'] && foo['respResult']!='' ){
							GLOBAL[cache_apiId] = cache_serialize;
							funcAppendSelectOptions(cache_apiId);
							$('#id_select').val('-1');
						}
					}else{}
				}else{}
			},
			error: function(err){
				console.log(err);
			}
		});
	});
	
	
	// btn_load
	$('#btn_load').unbind();
	$('#btn_load').on('click',function(){
		var apiId = $('#id_apiId').val();
		var appSubId = $('#id_appSubId').val();
		var env = $('#id_hidden_env').val();
		if(apiId && $.trim(apiId)!='' && appSubId && $.trim(appSubId)!='' ){
			$.ajax({
				url: ctx+'/common/index/testStepLoad',
				data: {"apiId":apiId, "appSubId":appSubId, "env":env},
				type: 'post',
				dataType: 'json',
				success: function(data){
					console.log(data);
					if(data){
						if(data['errorCode'] == '0'){
							var foo = data['result'];
							$('#id_appAppSecret').val( foo['app_secret'] );
							$('#id_apiId').val( foo['api_id'] );
							$('#id_apiVersion').val( foo['version'] );
							$('#id_appSubId').val( foo['app_sub_id'] );
							$('#id_partnerId').val( foo['org_id'] );
							$('#id_appPubId').val( foo['app_pub_id'] );
							$('#id_sysId').val( foo['sys_id'] );
							$('#id_appToken').val( foo['token'] );
						}else{}
					}else{}
				},
				error: function(err){
					console.log(err);
				}
			});
		}
		
	});
	
	//如果是RSA时显示填写"privateKey"的输入框
	$.each( $('input[name="signMethod"]') , function(i,n){
		$(n).unbind();
		$(n).on('click', function(){
			if( $(n).val() == "RSA" ){
				$('#div_privateKey').removeClass('div-hidden');
			}else{
				$('#div_privateKey').addClass('div-hidden');
			}
		});
	});
	
	
	//下拉框change事件
	$('#id_select').unbind();
	$('#id_select').on('change', function(i,n){
		var val = $('#id_select').val();
		// GLOBAL[cache_apiId]
		if( GLOBAL[val] ){
			var obj = funcTranslateToJson(GLOBAL[val]);
			$.each($('input[name="Format_aa"]'), function(i,n){
				if($(n).val() == obj['format']){
					$(n).trigger('click');
				}
			});
			$.each($('input[name="reqType"]'), function(i,n){
				if($(n).val() == obj['reqType']){
					$(n).trigger('click');
				}
			});
			$.each($('input[name="signMethod"]'), function(i,n){
				if($(n).val() == obj['signMethod']){
					$(n).trigger('click');
				}
			});
			$('#id_appAppSecret').val( obj['appAppSecret'] );
			
			$('#id_hidden_url').val( obj['url'] );
			$('#id_url').empty();
			$('#id_url').text( obj['url'] );
			
			var timeStamp = obj['timeStamp'].replace('+', ' ');
			$('#id_hidden_Time_Stamp').val( timeStamp );
			$('#id_Time_Stamp').empty();
			$('#id_Time_Stamp').text( timeStamp );
			
			$('#id_apiId').val( obj['apiId'] );
			$('#id_apiVersion').val( obj['apiVersion'] );
			$('#id_appSubId').val( obj['appSubId'] );
			$('#id_partnerId').val( obj['partnerId'] );
			$('#id_appPubId').val( obj['appPubId'] );
			$('#id_sysId').val( obj['sysId'] );
			$('#id_appToken').val( obj['appToken'] );
			
			$('#id_reqdata').val( obj['reqdata'] );
			
			//Add at 2017年4月21日11:00:26
			$.each($('input[name="env_radio"]'), function(i,n){
				if($(n).val() == obj['env']){
					$(n).trigger('click');
				}
			});
			$('#id_hidden_env').val( obj['env'] );
		}
	});
	
	//模拟点击
	$.each( $('input[name="reqType"]'), function(i,n){
		if( $(n).val() == 'Soap' ){
			$(n).trigger('click');
		}
	} );
	$.each( $('input[name="reqType"]'), function(i,n){
		if( $(n).val() == 'Rest' ){
			$(n).trigger('click');
		}
	} );
	$.each($('input[name="Format_aa"]'), function(i,n){
		if($(n).val()=='json'){
			$(n).trigger('click');
		}
	});
	
	//
	
});